package com.huawei.weather.controller;

import com.huawei.weather.client.WeatherdataClient;
import com.huawei.weather.domain.City;
import com.huawei.weather.domain.WeatherResponse;
import com.huawei.weather.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
public class WeatherController {
    @Autowired
    WeatherdataClient weatherdataClient;

    @Autowired
    CityService cityService;

    @RequestMapping("show/{cityId}")
    public ModelAndView show(@PathVariable("cityId") String cityId){
        //1.获取查询结果   2.实例化ModelAndView对象   3.跳转到目标页面
        WeatherResponse weatherResponse=weatherdataClient.findWeatherByUser(cityId);
        List<City> cityList=cityService.findAll();
        System.out.println("cityList:"+cityList.size());

        ModelAndView mav=new ModelAndView();
        mav.addObject("weatherData",weatherResponse);
        mav.addObject("cityList",cityList);
        mav.setViewName("weather");  //  emp.html  视图解析器： classpath:/templates/前缀+emp+后缀.html

        return mav;

    }

}
